package com.dee.components.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Base64;

import com.dee.components.LogUtils;

import java.io.ByteArrayOutputStream;


/**
 * Created by kkan on 2016/1/24.
 * 图像工具类
 *
 * 单位转换
 * dpToPx                       : dp转px
 * dip2px                      : dip转px
 * pxTODp                       : px转dp
 * px2dip                      : px转dip
 * sp2px                       : sp转px
 * px2sp                       : px转sp
 */

public class ImageUtils {

    /**
     * dip转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public static int dipToPx(Context context,float dpValue) {
        return dpToPx(context,dpValue);
    }

    /**
     * dp转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public static int dpToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * px转dip
     *
     * @param pxValue px值
     * @return dip值
     */
    public static int pxToDip(Context context,float pxValue) {
        return pxToDp(context,pxValue);
    }

    /**
     * px转dp
     *
     * @param pxValue px值
     * @return dp值
     */
    public static int pxToDp(Context context,float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * sp转px
     *
     * @param spValue sp值
     * @return px值
     */
    public static int spToPx(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * px转sp
     *
     * @param pxValue px值
     * @return sp值
     */
    public static int pxToSp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }


    /**
     * Resize the bitmap
     *
     * @param bitmap 图片引用
     * @param width 宽度
     * @param height 高度
     * @return 缩放之后的图片引用
     */
    public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) width / w);
        float scaleHeight = ((float) height / h);
        matrix.postScale(scaleWidth, scaleHeight);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
    }
    public static Bitmap fileToBitmap(String filePath) {
        return BitmapFactory.decodeFile(filePath);
    }

    public static Bitmap fileToBitmap(String filePath,int inSampleSize) {
        return BitmapFactory.decodeFile(filePath,getBitmapOption(inSampleSize)); //将图片的长和宽缩小味原来的1/2
    }

    public static String fileToBase64(String path, int targetSizeKb) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(path, options);
        // Calculate inSampleSize
//        options.inSampleSize = calculateInSampleSize(options, 800, 800);
        options.inSampleSize = calculateInSampleSize(options, 480, 480);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        Bitmap bitmap = BitmapFactory.decodeFile(path, options);


        return bitmap2StrByBase64(bitmap,targetSizeKb);
    }

    /**
     * 压缩bitmap到指定大小
     * @param bitmap
     * @param targetSizeKb
     * @return
     */
    public static String bitmap2StrByBase64(Bitmap bitmap, int targetSizeKb) {

        int quality = 100;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);

        // Check if image is bigger than targetSizeKb
        while ((outputStream.toByteArray().length / 1024) > targetSizeKb && quality > 0) {
            outputStream.reset();
            quality -= 5;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        }
        LogUtils.eTag("ImageUtils","压缩后大小=>>"+(outputStream.toByteArray().length / 1024));

        byte[] bytes = outputStream.toByteArray();
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

    private static BitmapFactory.Options getBitmapOption(int inSampleSize){
        System.gc();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        options.inSampleSize = inSampleSize;
        return options;
    }
}
